package com.test.springCloud.controller;

import com.alibaba.fastjson.JSONObject;
import com.test.springCloud.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 文章控制层
 */
@Controller
@CrossOrigin  //跨域
@RequestMapping("/lxf")
@Slf4j
public class ArticleController {

    @Resource
    ArticleService articleService;

    /**
     * 添加文章
     * @param req
     * @param request
     * @return
     */
    @ResponseBody
    @PostMapping("/addArticle")
    public String addArticle(@RequestBody JSONObject req, HttpServletRequest request){
       return articleService.addArticle(req,request);
    }

    /**
     * 获取所有文章内容
     * @return
     */
    @ResponseBody
    @GetMapping("/getAllArticle")
    public String getAllArticle(@RequestParam("page") Integer page,@RequestParam("limit") Integer limit){
        return articleService.getAllArticle(page,limit);
    }

    /**
     * 根据id获取文章内容
     * @return
     */
    @ResponseBody
    @GetMapping("/getArticleByid")
    public String getArticleByid(@RequestParam("page") Integer page,@RequestParam("limit") Integer limit,@RequestParam("newsClassId") Integer newsClassId){
        return articleService.getArticleByid(page,limit,newsClassId);
    }

    /**
     * 根据文章id编辑内容
     * @return
     */
    @ResponseBody
    @PostMapping("/updateArticleById")
    public String updateArticleById(@RequestBody JSONObject req, HttpServletRequest request){
        return articleService.updateArticleByid(req,request);
    }

    /**
     * 根据文章id删除内容
     * @return
     */
    @ResponseBody
    @PostMapping("/deleteArticleById")
    public String deleteArticleById(@RequestBody JSONObject req, HttpServletRequest request){
        return articleService.deleteArticleById(req,request);
    }
}
